<?php
namespace app\admin\model;
use think\Model;
use think\facade\Db;
class Company extends Model {
    use \mod\Company;

    //更新前
    public static function onBeforeUpdate($data)
    {
        //更新数据表
        if (isset($data['thumbFile'])) {
            unset($data['thumbFile']);
        }
        unset($data['create_time']);
        unset($data['update_time']);

        return $data;
    }
    public static function onAfterUpdate($data)
    {
        if(!empty($data->area)){
            self::setArea($data);
        }
        if((isset($data->status) && $data->status != '') || !empty($data->endtime)){
            event('update_server',['company_id'=>$data->id]);
        }
    }
    //写入后
    public static function onAfterWrite($data){
        //相册图片绑定处理
        if($data['thumb']){
            Files::bindInfo($data['thumb'],$data['id'],0,'company','thumb');
        }
        self::setArea($data);
    }
    //自定义内容
    public function getThumbFileAttr($value,$data){
        if(isset($data['thumb'])){
            $res = Files::where('id',$data['thumb'])->find();
            if(!isset($res->filepath)){
                return '';
            }
            return $res->filepath;
        }   
    }
    public function getAreaNameAttr($value,$data){
        if($data['area']){
            $name = Classify::Where('id','in',json_decode($data['area']))->column('title');
            return implode('',$name);
        }
    }
    public function getstatusAttr($value,$data){
        if(isset($data['endtime']) && !empty($data['endtime'] && $data['endtime'] < time())){
            return -1;
        }else{
            return $value;
        }
    }
    public function companyOrder(){
        return $this->hasMany(Order::class);
    }
    public function companyServer(){
        return $this->hasOne(Server::class);
    }

    public function companyArea(){
        return $this->hasMany(Area::class);
    }
    public function getAreaAttr($value,$data){
        if($value){
            $value=json_decode($value);
        }
        return $value;
    }
    public function server()
    {
        return $this->hasOne(Server::class);
    }
    //结束日期设置
    public function setEndtimeAttr($value,$data){
        return $value?strtotime($value):0;
    }
    public function getEndtimeAttr($value,$data){
        return $value?date("Y-m-d H:i:s",$value):0;
    }


    public static function setArea($data){
        $id=(int)$data->id;
        $area=$data->area;
        if(!is_array($area)) return;
        Area::where('company_id',$id)->delete();
        $insertData=[];
        foreach($area as $k=>$v){
            $item=[];
            $item['company_id']=(int)$id;
            $item['area_id']=(int)$v;
            $insertData[]=$item;
        }
        Db::name('area')->insertAll($insertData);
    }
    public function getAdminIdNameAttr($value,$data){
        if(!empty($data['admin_id'])){
            $admin_name = Admin::where('id',$data['admin_id'])->value('truename');
        }else{
            $admin_name = '';
        }
        return $admin_name;
    }

}
?>